﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Models;
using VietPremier.Repository;
using System.Text;

namespace VietPremier.Controllers
{
    public class InquiryController : Controller
    {
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                InquiryModel model = new InquiryModel();
                //SEO
                EntitySEO seo = db.EntitySEOs.FirstOrDefault(o => o.Type == "Inquiry");
                model.Titletag = seo.Titletag;
                model.Meta_description = seo.Meta_description;
                model.Meta_keyword = seo.Meta_keyword;

                //using loop to get all Style in EntityStyle
                foreach (var style in db.EntityStyles)
                {
                    model.listStyle.Add(new Style()
                    {
                        Id = style.Id,
                        Name = style.Name
                    });
                }
                model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                return View(model);
            }            
        }


        [HttpPost]
        public ActionResult Index(InquiryModel model)
        {
            if (ModelState.IsValid)
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    StringBuilder mailBody = new StringBuilder();
                    mailBody.AppendFormat("<h1>Inquiry Information</h1>");
                    mailBody.AppendFormat("<table style='height:410px; width:749px'>");          
                    mailBody.AppendFormat("<tbody><tr><td>Contact Name:</td><td>" + model.Gender + " " + model.FullName + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Đơn vị/ Doanh nghiệp:</td><td>" + model.Supplier + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Nơi đến:</td><td>" + model.Destination + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Quốc tịch:</td><td>" + model.Nationality + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Điện thoại:</td><td>" + model.Telephone + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Email:</td><td>" + model.Email + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Number of Adults:</td><td>" + model.NumberofAdults + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Number of Children:</td><td>" + model.NumberofChildren + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>From:</td><td>" + model.DateFrom + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>To:</td><td>" + model.DateTo + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Hotel standard:</td><td>" + model.Place + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Special Request:</td><td>" + model.SpecialRequest + "</td></tr></tbody></table>");
                    mailBody.AppendFormat("<p>Email được gửi từ một email ảo, No reply</p>");

                    try
                    {
                        MailHelper.SendMailMessage("noreply@khainguyenepco.vn", "sales@vietnampremier.com", "hmduc@khainguyenepco.vn", "khoa@vietnampremier.com", "subject", mailBody.ToString(), "smtp.gmail.com", true, "noreply@khainguyenepco.vn", "kh@inguyen");
                        EntityFeedBack feedback = new EntityFeedBack();
                        feedback.Email = model.Email;
                        feedback.DateCreate = DateTime.Now;
                        feedback.Body = mailBody.ToString();
                        feedback.Page = "Inquiry";
                        db.EntityFeedBacks.InsertOnSubmit(feedback);
                        db.SubmitChanges();

                        TempData["Message"] = "Success";
                    }
                    catch
                    {
                        TempData["Message"] = "ErrorMessage";
                    }

                    model = new InquiryModel();

                    //using loop to get all Style in EntityStyle
                    foreach (var style in db.EntityStyles)
                    {
                        model.listStyle.Add(new Style()
                        {
                            Id = style.Id,
                            Name = style.Name
                        });
                    }
                    model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                    return View(model);
                }

            }
            else
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    //using loop to get all Style in EntityStyle
                    foreach (var style in db.EntityStyles)
                    {
                        model.listStyle.Add(new Style()
                        {
                            Id = style.Id,
                            Name = style.Name
                        });
                    }
                    model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");
                    return View(model);
                }
            }
         }

    }
}
